<?php
/**
 * Created by PhpStorm.
 * User: 王崇全
 * Date: 2018-12-18 11:03:15
 */
namespace app\api\controller;


use app\common\controller\BaseController;
use app\common\model\Article;
use app\common\model\ArticleCategory;

/**
 * Class ArticleController
 * @author  王崇全
 * @date    2018/12/18 11:03
 * @package app\api\controller
 */
class ArticleController extends BaseController
{

    /**
     * 栏目列表
     * @author 王崇全
     * @date   2018/12/18 11:59
     * @return void
     */
    public function listCatalog()
    {
        $in = $this->I([
            [
                "pid|上级栏目ID",
                null,
                "d",
            ]
        ]);

        $ac   = new ArticleCategory();
        $list = $ac->categoryList($in["pid"]);

        rjd($list);
    }

    /**
     * 文章信息
     * @author 王崇全
     * @date   2018/12/20 10:44
     * @return void
     * @throws \Exception
     */
    public function info()
    {
        $in = $this->I([
            [
                "id|文章ID",
                null,
                "s",
                "require",
            ],
        ]);

        $a    = new Article();
        $info = $a->getInfo($in["id"]);

        rjd($info);
    }

    /**
     * 文章列表
     * @author 王崇全
     * @date   2018/12/20 10:44
     * @return void
     */
    public function listArticle()
    {

        $in = $this->I([
            [
                "cid|栏目ID",
                null,
                "d",
            ],
            [
                "title|标题关键词",
                null,
                "s",
            ],
            [
                "st|起始时间",
                null,
                "s",
            ],
            [
                "et|截止时间",
                null,
                "s",
            ],
            [
                "pageNo|页码",
                DEF_PAGE_NO,
                "d",
            ],
            [
                "pageSize|页幅",
                DEF_PAGE_SIZE,
                "d",
            ],
        ]);

        $a    = new Article();
        $list = $a->getList([
            "cid"   => $in["cid"],
            "st"    => $in["st"],
            "et"    => $in["et"],
            "title" => trim($in["title"]),
        ], $in["pageNo"], $in["pageSize"]);

        //梳理内容
        foreach ($list[0] as &$v) {
            $v["content"] = htmlspecialchars_decode($v["content"]);//把一些预定义的 HTML 实体转换为字符
            $v["content"] = str_replace("&nbsp;", "", $v["content"]);//将空格替换成空
            $v["content"] = strip_tags($v["content"]);//函数剥去字符串中的 HTML、XML 以及 PHP 的标签,获取纯文本内容
            $v["content"] = mb_substr($v["content"], 0, 40, "utf-8");//返回字符串中的前100字符串长度的字符
            if (mb_strlen($v["content"]) == 40) {
                $v["content"] .= "...";
            }
        }

        rjd([
            "list"  => $list[0],
            "count" => $list[1],
        ]);
    }

}